<?php
	require_once('init.php');	
	
	//Get Column List
    $columns = $adminDB->executeSQL("select id,fdName from ".$util->contentdbName().".tbColumn order by fdOrder", $connID);
    for ($i = 0; $i < count($columns); $i ++) {
        $arrayColumnOption[$columns[$i]['id']] = $columns[$i]['fdName'];
    }
    $smarty->assign('arrayColumnOption', $arrayColumnOption);
	
	//Isset Post Action
	if (isset($_POST['name']) && $_POST['name'] != '') 
    {		
		//Query Product Name
        if (! $adminDB->executeSQL("select fdName from ".$util->contentdbName().".tbProduct where fdName ='".trim($_POST['name'])."'", $connID)) 
        {			
			//Try Insert Conent
            if(!$adminDB->executeSQL("insert into ".$util->contentdbName().".tbContent (fdName,fdTypeID,fdOperatorID,fdApproverID,fdApprove,fdCreate,fdAdapt,fdBornID,fdCommentID) values('".trim($_POST['name'])."', '1','".$_SESSION['adminid']."', '" .$_SESSION['adminid']."',now(),now(),'0','0','0')", $connID))
            {
                echo "<script>alert('产品Content添加失败！');</script>";
            }
            else
            {
				//Query Max ContnetID
				$contentid = mysql_insert_id();
				//Try Insert Index
                $adminDB->executeSQL("insert into ".$util->contentdbName().".tbIndex (fdContentID,fdColumnID,fdOrder) values('".$contentid."', '".trim($_POST['column'])."','0')", $connID);
                
				//Try Insert Product
				if (!$adminDB->executeSQL("insert into ".$util->contentdbName().".tbProduct(fdName,fdTarget,fdListPrice,fdPrice,fdAvailabe,fdExpire,fdDescription,fdMemo,fdContentID,fdLicense,fdVersion) values('".trim($_POST['name'])."', '".trim($_POST['target'])."','".trim($_POST['listprice'])."', '".trim($_POST['price'])."','".$_POST['availabe']."','".$_POST['expire']."','".trim($_POST['description'])."','".trim($_POST['memo'])."','".$contentid."','".trim($_POST['license'])."','".trim($_POST['version'])."')", $connID)) 
                {			
                    echo "<script>alert('产品信息添加失败！');</script>";			
                } 
                else {
					//Insert AdminLog
        			$adminDB->executeSQL("insert into ".$util->userdbName().".tbAdminLog (fdOperatorID,fdAction,fdTarget,fdContentID,fdTime) values('".$_SESSION['adminid']."', 'INSERT', '". $util->contentdbName() .".tbProduct(fdName,fdTarget,fdListPrice,fdPrice,fdAvailabe,fdExpire,fdDescription,fdMemo,fdContentID) values(".trim($_POST['name']).", ".trim($_POST['target']).",".trim($_POST['listprice']).", ".trim($_POST['price']).",".$_POST['availabe'].",".$_POST['expire'].",".trim($_POST['description']).",".trim($_POST['memo']).",".$contentid.")' ,".$contentid.", now())", $connID);
        			echo "<script>alert('产品信息添加成功！');</script>";
                }
            }
        } 
        else 
        {
            echo "<script>alert('已存在同名产品！请保证产品名不重复');</script>";
        }
    }
	
	$smarty->display('admin_addproduct.html');	
	$connDB->closeConnID();
	
?>